package com.xiaochong.bitong.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.xiaochong.bitong.api.constant.ReturnCode;
import com.xiaochong.bitong.api.service.WeightService;
import com.xiaochong.bitong.api.utils.ApiVersion;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.hbasepo.WeightIndex;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.app.AppWeightCurrencyInfo;
import com.xiaochong.tir.common.data.vo.app.AppWeightInfo;
import com.xiaochong.tir.common.util.ApiResultCodeUtils;
import com.xiaochong.tir.common.util.AppRedisKeyUtils;
import com.xiaochong.tir.common.util.RedisKeysUtils;

/**
 * @author qzw
 * 2018年8月6日 下午7:58:36
 * @description 
 **/
@RestController
@Api(value = "权重货币信息")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/weight")
public class WeightController {
	
	@Autowired
	private WeightService weightService;
	
	@Autowired
	 private RedisServer rediscConfig;

	@ApiOperation(value = "APP权重指数 - 权重信息", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/getWeightIndex")
	public ApiResultVo<AppWeightInfo> getWeightIndex(){
		ApiResultVo<AppWeightInfo> apiResultVo = new ApiResultVo<AppWeightInfo>();
		String data = rediscConfig.get(AppRedisKeyUtils.APP_WEIGHT_INFO);
		if(data == null || "".equals(data.trim())){
			apiResultVo.setCode(ReturnCode.DATA_NOT_EXIST);
			apiResultVo.setMessage(ApiResultCodeUtils.SYSTEM_NULL.getMsg());
			return apiResultVo;
		}
		AppWeightInfo appWeightInfo = JSON.parseObject(data, AppWeightInfo.class);
		String grailIndex = rediscConfig.get(RedisKeysUtils.TIR_WEIGHT_INDEX);
		 if(StringUtils.isNotBlank(grailIndex)){
			 WeightIndex bi = JSON.parseObject(grailIndex,WeightIndex.class);
			 appWeightInfo.setWeightIndex(bi.getWeightIndex()!=null?bi.getWeightIndex().setScale(2,BigDecimal.ROUND_DOWN).toString():"");
			 appWeightInfo.setWeightRiseFall(bi.getWeightRiseFall()!=null?bi.getWeightRiseFall().multiply( new BigDecimal( 100 ) ).setScale(2,BigDecimal.ROUND_DOWN):new BigDecimal(0.00).setScale(2,BigDecimal.ROUND_DOWN));
			 appWeightInfo.setIndexValue(new BigDecimal(appWeightInfo.getWeightIndex()).multiply(appWeightInfo.getWeightRiseFall()).divide(new BigDecimal(100), 2,BigDecimal.ROUND_DOWN));
		 }
		apiResultVo.setCode(ReturnCode.SUCCESS);
		apiResultVo.setMessage(ApiResultCodeUtils.BASE_SUCCESS.getMsg());
		apiResultVo.setData(appWeightInfo);
		return apiResultVo;
	}
	
	@ApiOperation(value = "APP权重指数 - 货币信息", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
	@ApiImplicitParams(
			{@ApiImplicitParam(name = "column", value = "排序字段（市值：marketCapCny, 最新价格：newPriceCny， 24小时涨幅：rise_24h）", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "mark", value = "大到小：desc，小到大：asc", required = true, dataType = "String", paramType = "query")})
	@PostMapping("/getWeightCurrencyInfo")
	public ApiResultVo<List<AppWeightCurrencyInfo>> getWeightCurrencyInfo(@RequestParam(value = "column", required = true) String column,
			@RequestParam(value = "mark", required = true) String mark){
		ApiResultVo<List<AppWeightCurrencyInfo>> apiResultVo = new ApiResultVo<List<AppWeightCurrencyInfo>>();
		if(column == null || "".equals(column.trim()) || mark == null || "".equals(mark.trim())){
			apiResultVo.setCode(ReturnCode.PARAMS_ERROR);
			apiResultVo.setMessage(ApiResultCodeUtils.PARAMS_ERROR.getMsg());
			return apiResultVo;
		}
		List<AppWeightCurrencyInfo> list = weightService.getCurrencyInfo();
		if(list == null || list.size() == 0){
			apiResultVo.setCode(ReturnCode.DATA_NOT_EXIST);
			apiResultVo.setMessage(ApiResultCodeUtils.SYSTEM_NULL.getMsg());
			return apiResultVo;
		}
		weightService.sortList(column, mark, list);
		apiResultVo.setCode(ReturnCode.SUCCESS);
		apiResultVo.setMessage(ApiResultCodeUtils.BASE_SUCCESS.getMsg());
		apiResultVo.setData(list);
		return apiResultVo;
	}
	
	@ApiOperation(value = "APP权重指数 - 价格指数曲线图", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
	@ApiImplicitParams({@ApiImplicitParam(name = "time", value = "时间维度参数(3h,1d,7d,1m,1y)", required = true, dataType = "String", paramType = "query")})
	@PostMapping("/getWeightIndexInfo")
	public ApiResultVo<List<AppWeightInfo>> getWeightIndexInfo(@RequestParam(value = "time", required = true) String time){
		ApiResultVo<List<AppWeightInfo>> apiResultVo = new ApiResultVo<List<AppWeightInfo>>();
		if(time == null || "".equals(time.trim())){
			apiResultVo.setCode(ReturnCode.PARAMS_ERROR);
			apiResultVo.setMessage(ApiResultCodeUtils.PARAMS_ERROR.getMsg());
			return apiResultVo;
		}
		List<AppWeightInfo> list = new ArrayList<>();
		if("3h".equals(time)){
			String data = rediscConfig.get(AppRedisKeyUtils.APP_WEIGHT_3HOUR);
			list = JSON.parseObject(data, List.class);
		}else if("1d".equals(time)){
			String data = rediscConfig.get(AppRedisKeyUtils.APP_WEIGHT_1DAY);
			list = JSON.parseObject(data, List.class);
		}else if("7d".equals(time)){
			String data = rediscConfig.get(AppRedisKeyUtils.APP_WEIGHT_7DAY);
			list = JSON.parseObject(data, List.class);
		}else if("1m".equals(time)){
			String data = rediscConfig.get(AppRedisKeyUtils.APP_WEIGHT_1MONTH);
			list = JSON.parseObject(data, List.class);
		}else if("1y".equals(time)){
			String data = rediscConfig.get(AppRedisKeyUtils.APP_WEIGHT_1YEAR);
			list = JSON.parseObject(data, List.class);
		}
		if(list == null || list.size() == 0){
			apiResultVo.setCode(ReturnCode.DATA_NOT_EXIST);
			apiResultVo.setMessage(ApiResultCodeUtils.SYSTEM_NULL.getMsg());
			return apiResultVo;
		}
		apiResultVo.setCode(ReturnCode.SUCCESS);
		apiResultVo.setMessage(ApiResultCodeUtils.BASE_SUCCESS.getMsg());
		apiResultVo.setData(list);
		return apiResultVo;
	}
}
